# -*- coding:utf-8 -*-
#作者：琴弦上的宇宙
#时间：2021年10月22日

import os
from common import *
from openCv import *

class scPveSelectLevel(myScene):
    def __init__(self,bValid):
        self.bValid=bValid
        self.timeContinue=time.time()
        self.offset1=[[-260,190],[-30,190],[200,190],[-260,460],[-30,460],[200,460],\
                      [-170,210],[120,260],[-50,470]]
        self.offset2=[[-260,190],[-30,190],[200,190],[-260,460],[-30,460],[200,460]]
        self.offset3=[[-260,190],[-30,190],[200,190],[-260,460],[-30,460],[200,460]]
        self.offset4=[[-260,190],[-30,190],[200,190],[-260,460],[-30,460],[200,460],\
                      [-260,190],[-30,190],[200,190],[-260,460],[-30,460],[200,460],\
                      [0,260]]
        self.zone1Pos=[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],\
                       [0,0],[0,0],[0,0]]
        self.zone2Pos=[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
        self.zone3Pos=[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
        self.zone4Pos=[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],
                       [0,0],[0,0],[0,0],[0,0],[0,0],[0,0],
                       [0,0]]
        self.name='scPveSelectLevel'
        self.path='resource/mercenary/pve/scPveSelectLevel'
        self.tagPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('tag')]
        self.funcPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('func')]

    def enable(self):
        self.bValid=True

    def dis(self):
        self.bValid=False

    def isOwn(self,background):
        if self.bValid:
            tagList={}
            for tag in self.tagPng:
                bFind,x,y,w,h=bFindInBackground(background,tag,0.70)
                if bFind:
                    tagList[tag.name]=(x,y,w,h)
            if ('tagReward' in tagList):
                pos=tagList['tagReward']
                zone=None
                if MyGui.bTaskRunning:
                    zone=int((MyGui.gTaskLevel.replace('\n','').split('-'))[0])
                else:
                    zone=int((MyGui.gLevel.replace('\n','').split('-'))[0])
                if zone==1:
                    for i in range(9):
                        self.zone1Pos[i][0]=pos[0]+self.offset1[i][0]
                        self.zone1Pos[i][1]=pos[1]+self.offset1[i][1]
                elif zone==2:
                    for i in range(6):
                        self.zone2Pos[i][0]=pos[0]+self.offset2[i][0]
                        self.zone2Pos[i][1]=pos[1]+self.offset2[i][1]
                elif zone==3:
                    for i in range(6):
                        self.zone3Pos[i][0]=pos[0]+self.offset3[i][0]
                        self.zone3Pos[i][1]=pos[1]+self.offset3[i][1]
                elif zone==4:
                    for i in range(13):
                        self.zone4Pos[i][0]=pos[0]+self.offset4[i][0]
                        self.zone4Pos[i][1]=pos[1]+self.offset4[i][1]
                return True
            else:
                return False
        else:
            return False

    def proc(self,background):

        #构造数据
        zone=None
        level=None
        pos=None
        if MyGui.bTaskRunning:
            zone=int((MyGui.gTaskLevel.replace('\n','').split('-'))[0])
            level=int((MyGui.gTaskLevel.replace('\n','').split('-'))[1])
        else:
            zone=int((MyGui.gLevel.replace('\n','').split('-'))[0])
            level=int((MyGui.gLevel.replace('\n','').split('-'))[1])
        if zone==1:     pos=self.zone1Pos[level-1]
        elif zone==2:   pos=self.zone2Pos[level-1]
        elif zone==3:   pos=self.zone3Pos[level-1]
        elif zone==4:   pos=self.zone4Pos[level-1]

        #循环
        bg=background
        while True:

            #需要重新选择关卡，去1-1做任务
            #做任务时，定时1小时重新进入村庄避免卡任务
            bNeedBack=False
            if MyGui.bTaskLevel or \
                ((time.time()-self.timeContinue)>MyGui.G_TASK_TIME and MyGui.gViewTask):
                bNeedBack=True
            if bNeedBack:
                for func in self.funcPng:
                    if func.name=='funcBack':
                        bFind,x,y,w,h=bFindInBackground(bg,func,0.90)
                        if bFind:
                            moveAndClick(x+w/2,y+h/2,3)
                            bg=SaveScreen()
                            if not self.isOwn(bg):
                                self.timeContinue=time.time()
                                if MyGui.bTaskLevel:MyGui.bTaskLevel=False
                                MyGui.gWait=2
                                time.sleep(2)
                            return True

            #选择小关
            bStart=False
            if zone==2 or zone==3:
                moveAndClick(pos[0],pos[1])
                bStart=True
            elif zone==1 or zone==4:
                bFindPre=False
                bFindNext=False
                posPre=None
                posNext=None
                for func in self.funcPng:
                    if func.name=='funcPre':
                        bFind,x,y,w,h=bFindInBackground(bg,func,0.90)
                        if bFind:
                            bFindPre=True
                            posPre=[x,y,w,h]
                            break
                for func in self.funcPng:
                    if func.name=='funcNext':
                        bFind,x,y,w,h=bFindInBackground(bg,func,0.90)
                        if bFind:
                            bFindNext=True
                            posNext=[x,y,w,h]
                            break
                if zone==4:
                    if level>=1 and level<=6:
                        if bFindNext and not bFindPre:
                            moveAndClick(pos[0],pos[1],2)
                            bStart=True
                        else:
                            if bFindPre:
                                moveAndClick(posPre[0]+posPre[2]/2,posPre[1]+posPre[3]/2,2)
                    elif level>=7 and level<=12:
                        if bFindNext and bFindPre:
                            moveAndClick(pos[0],pos[1],2)
                            bStart=True
                        elif bFindPre:
                            moveAndClick(posPre[0]+posPre[2]/2,posPre[1]+posPre[3]/2,2)
                        elif bFindNext:
                            moveAndClick(posNext[0]+posNext[2]/2,posNext[1]+posNext[3]/2,2)
                    elif level>=13 and level<=13:
                        if not bFindNext and bFindPre:
                            moveAndClick(pos[0],pos[1],2)
                            bStart=True
                        else:
                            if bFindNext:
                                moveAndClick(posNext[0]+posNext[2]/2,posNext[1]+posNext[3]/2,2)
                else:
                    if level>=1 and level<=6:
                        if bFindNext and not bFindPre:
                            moveAndClick(pos[0],pos[1],2)
                            bStart=True
                        else:
                            if bFindPre:
                                moveAndClick(posPre[0]+posPre[2]/2,posPre[1]+posPre[3]/2,2)
                    elif level>=7 and level<=9:
                        if not bFindNext and bFindPre:
                            moveAndClick(pos[0],pos[1],2)
                            bStart=True
                        else:
                            if bFindNext:
                                moveAndClick(posNext[0]+posNext[2]/2,posNext[1]+posNext[3]/2,2)

            #开始对战
            if bStart:
                bg=SaveScreen()
                for func in self.funcPng:
                    if func.name=='funcStart':
                        bFind,x,y,w,h=bFindInBackground(bg,func,0.90)
                        if bFind:
                            moveAndClick(x+w/2,y+h/2,2)
                            return True

            #重新截图判断
            bg=SaveScreen()
            if not self.isOwn(bg):
                return False
